<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Punish;
use App\Model\PunishDetail;
use Auth;

class PunishController extends Controller
{
    /*
    *处罚列表
    *@return 列表页面 
    */
    public function listt(Request $request)
    {
        $title = $request->input('title','');

        $where = array();
        if(!empty($title)) {
            $where[] = array('title', 'like', '%'.$title.'%');
        }
        if(!empty($where)) {
            $list = Punish::where($where)->orderBy('updateTime', 'DESC')->get();
        } else {
            $list = Punish::orderBy('updateTime', 'DESC')->get(); 
        }
        if(!empty($title)) {
            foreach($list as $k=>$v) {
                $list[$k]['title'] = preg_replace("/($title)/i", 
                                "<font color=red><b>\\1</b></font>", 
                                $v['title']); 
            }
        }
        foreach($list as $k=>$v) {
            $list[$k]['updateTime'] = date('Y-m-d', $v['updateTime']);
        }

        return view('punish/list', [
            'list' => $list,
            'title' => $title
        ]);
    }

    /*
    *处罚详情
    *@param id 处罚id
    */
    public function detail(Request $request)
    {
        $id = $request->input('id');
        
        $punish = Punish::where('id', $id)->first();

        $punish_detail = PunishDetail::where('id', $id)->first();
        $punish_detail['content'] = html_entity_decode($punish_detail['content']);

        /*浏览量+1*/
        Punish::where('id', $id)->update(array('hits'=>$punish['hits']+1));

        return view('punish/detail', [
            'punish' => $punish,
            'punish_detail' => $punish_detail
        ]);
    }
   
}
